//
//  IdentitySureViewController.m
//  CommunityApp
//
//  Created by 秦乾龙 on 16/12/29.
//  Copyright © 2016年 秦乾龙. All rights reserved.
//

#import "IdentitySureViewController.h"
#import "ZYButton.h"
#import "NavView.h"
#import "WDData.h"
#import "IdentityThirdViewController.h"
#import "IdentitySecondViewController.h"
#import "Province.h"
#import "City.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScale   ([UIScreen mainScreen].bounds.size.width)/750
@interface IdentitySureViewController ()<UITextFieldDelegate,UITextViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain) UITableView *mainTableView;
@property (nonatomic,retain) UITableView *provinceTableView;
@property (nonatomic,retain) UITableView *cityTableView;
@property (nonatomic,copy) UITextField *nameField;
@property (nonatomic,copy) UITextField *ideNumField;
@property (nonatomic,copy) ZYButton *provinceBtn;
@property (nonatomic,copy) ZYButton *cityBtn;
@property (nonatomic,copy) UITextField *phoneNumField;
@property (nonatomic,copy) UITextField *codeField;
@property (nonatomic,copy) NSMutableArray *classNameArr;
@property (nonatomic,copy) ZYButton *codeBtn;
@property (nonatomic,copy) NSString *codeStr;
@property (nonatomic,copy) NSMutableArray *stepFinishedImgArr;
@property (nonatomic,copy) NSMutableArray *stepUnFinishedImgArr;
@property (nonatomic,copy) NSMutableArray *provinceArr;
@property (nonatomic,copy) NSMutableArray *cityArr;
@property (nonatomic,copy) UILabel *pTitleLable;
@property (nonatomic,copy) UILabel *cTitleLable;
@property (nonatomic,assign) BOOL provinceBtnSelect;
@property (nonatomic,assign) BOOL cityBtnSelect;
@property (nonatomic,copy) NSString *pId;
@property (nonatomic,copy) NSString *pName;
@property (nonatomic,copy) NSString *cName;


@end

@implementation IdentitySureViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _stepFinishedImgArr = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4", nil];
    _stepUnFinishedImgArr = [NSMutableArray arrayWithObjects:@"1灰",@"2灰",@"3灰",@"4灰", nil];
    _provinceArr = [NSMutableArray array];
    _cityArr = [NSMutableArray array];
    _provinceBtnSelect = NO;
    _cityBtnSelect = NO;
    _pId = @"18";
    [self setUI];
    
}
- (void) setUI
{
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"身份认证"];
    
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(backClick)];
    self.navigationItem.leftBarButtonItem = backBtn;
    
    UIButton *buttonNotice = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonNotice.frame = CGRectMake(0, 0, 63, 20);
    buttonNotice.titleLabel.font = [UIFont systemFontOfSize:13];
    [buttonNotice setTitle:@"下一步" forState:UIControlStateNormal];
    
    UIBarButtonItem *btnNoticeItem = [[UIBarButtonItem alloc]initWithCustomView:buttonNotice];
    [buttonNotice addTarget:self action:@selector(nextClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = btnNoticeItem;

    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,kScreenW , kScreenH-64) style:UITableViewStyleGrouped];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.tag = 20;
    [self.view addSubview:_mainTableView];
    _classNameArr = [NSMutableArray arrayWithObjects:@"真实姓名",@"身份证号",@"发货地区",@"手机号码",@"验证码", nil];
    
    _provinceTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    _provinceTableView.delegate = self;
    _provinceTableView.dataSource = self;
    _provinceTableView.tag = 21;
    _provinceTableView.separatorStyle = NO;
    [self.view addSubview:_provinceTableView];
    _cityTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    _cityTableView.delegate = self;
    _cityTableView.dataSource = self;
    _cityTableView.tag = 22;
    _cityTableView.separatorStyle = NO;
    [self.view addSubview:_cityTableView];
}
//PickerView代理方法
//- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
//{
//    return 1;
//}
//
//- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
//{
//    return 3;
//}
//
//-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
//{
//    NSString * title = nil;
//    switch (component) {
//        case 0:
//            title = @"河南";
//            break;
//        case 1:
//            title = @"";
//            break;
//        default:
//            break;
//    }
//    
//    return title;
//}
//tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView.tag==20) {
        return 6;
    }
    else
    {
        return 1;
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag==20) {
        return 1;
    }
    else if (tableView.tag==21)
    {
        return _provinceArr.count;
    }
    else if (tableView.tag==22)
    {
        return _cityArr.count;
    }

    return 0;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell =[[NSBundle mainBundle ]loadNibNamed:@"ZYTableViewCell" owner:nil options:nil][0];
    }
    //设置cell点击背景色不变
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (tableView.tag ==20) {
        if (indexPath.section==0)
        {
            UIView *stateView = [[UIView alloc]init];
            stateView.frame = CGRectMake(0, 0, kScreenW, 200*kScale);
            stateView.backgroundColor = [UIColor whiteColor];
            [cell addSubview:stateView];
            NSMutableArray *lableTextArr = [NSMutableArray arrayWithObjects:@"填充信息",@"上传照片",@"选择收款方式",@"提交审核", nil];
            for (int i = 0; i<4; i++)
            {
                CGFloat errorW = 5*kScale;
                CGFloat stateBtnH = 82*kScale;
                CGFloat lineViewW = (kScreenW - 60*kScale -4*stateBtnH)/3+errorW;
                CGFloat lineViewH = 3*kScale;
                ZYButton *stateBtn = [ZYButton buttonWithType:UIButtonTypeCustom];
                
                
                stateBtn.frame = CGRectMake(30*kScale+(stateBtnH+lineViewW-errorW)*i, 30*kScale, stateBtnH, stateBtnH);
                
                if (i==0)
                {
                    [stateBtn setImage:[UIImage imageNamed:_stepFinishedImgArr[i]] forState:UIControlStateNormal];
                }
                else
                {
                    [stateBtn setImage:[UIImage imageNamed:_stepUnFinishedImgArr[i]] forState:UIControlStateNormal];
                    UIView *lineView = [[UIView alloc]init];
                    lineView.frame = CGRectMake(30*kScale+(stateBtnH+lineViewW-errorW)*i-lineViewW+errorW/2, stateBtn.center.y, lineViewW, lineViewH);
                    stateBtn.lineView = lineView;
                    stateBtn.lineView.backgroundColor = ZallColorFromRGB(0x979797);
                    [stateView addSubview:lineView];
                }
                [stateView addSubview:stateBtn];
                
                UILabel *stateLable = [[UILabel alloc]init];
                stateLable.text = lableTextArr[i];
                stateLable.frame = CGRectMake(0, 0, kScreenW, 40*kScale);
                stateLable.center = CGPointMake(stateBtn.center.x, stateBtn.center.y+stateBtnH/2+31*kScale);
                stateLable.textAlignment = NSTextAlignmentCenter;
                stateLable.font = [UIFont systemFontOfSize:13];
                [stateView addSubview:stateLable];
            }
        }
        else
        {
            UILabel *classNameLable = [[UILabel alloc]init];
            classNameLable.text = [NSString stringWithFormat:@"%@:",_classNameArr[indexPath.section-1]];
            classNameLable.font = [UIFont systemFontOfSize:15];
            NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],};
            //        classNameLable.textColor = [UIColor colorWithRed:158/255.0 green:158/255.0 blue:158/255.0 alpha:1.0f];
            CGSize textSize = [@"真实姓名:" boundingRectWithSize:CGSizeMake(MAXFLOAT, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;
            classNameLable.frame = CGRectMake(30*kScale, 0, textSize.width, 100*kScale);
            [cell addSubview:classNameLable];
            if (indexPath.section==1)
            {
                _nameField = [[UITextField alloc]init];
                _nameField.delegate = self;
                _nameField.font = [UIFont systemFontOfSize:15];
                _nameField.frame = CGRectMake(30*kScale+textSize.width+58*kScale, 0, kScreenW-30*kScale-textSize.width-58*kScale, 100*kScale);
                _nameField.placeholder = @"请输入您的姓名";
                [cell addSubview:_nameField];
            }
            else if(indexPath.section==2)
            {
                _ideNumField = [[UITextField alloc]init];
                _ideNumField.delegate = self;
                _ideNumField.font = [UIFont systemFontOfSize:15];
                _ideNumField.frame = CGRectMake(30*kScale+textSize.width+58*kScale, 0, kScreenW-30*kScale-textSize.width-58*kScale, 100*kScale);
                _ideNumField.placeholder = @"请输入您的身份证号";
                _ideNumField.keyboardType = UIKeyboardTypeASCIICapable;
               
                [cell addSubview:_ideNumField];
            }
            else if(indexPath.section==3)
            {
                
                CGFloat btnW = 226*kScale;
                CGFloat btnH = 52*kScale;
                CGFloat titleW = 174*kScale;
                _provinceBtn = [ZYButton buttonWithType:UIButtonTypeRoundedRect];
                
                _provinceBtn.frame = CGRectMake(30*kScale+textSize.width+58*kScale, 24*kScale, btnW, btnH);
                //            [_provinceBtn setImage:[UIImage imageNamed:@"选择省份"] forState:UIControlStateNormal];
                [_provinceBtn setBackgroundImage:[UIImage imageNamed:@"选择省份"] forState:UIControlStateNormal];
                 [_provinceBtn setBackgroundImage:[UIImage imageNamed:@"选择省份"] forState:UIControlStateSelected];
                [_provinceBtn addTarget:self action:@selector(chooseProvince:) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:_provinceBtn];
                _pTitleLable = [[UILabel alloc]init];
                _pTitleLable.frame = CGRectMake(0, 0, titleW, btnH);
                _pTitleLable.text = @"湖北";
                _pTitleLable.font = [UIFont systemFontOfSize:13];
                _pTitleLable.textColor = ZallColorFromRGB(0x9e9e9e);
                _pTitleLable.textAlignment = NSTextAlignmentCenter;
                _pTitleLable.adjustsFontSizeToFitWidth = YES;
                [_provinceBtn addSubview:_pTitleLable];
                
                //            _provinceTableView = [[UITableView alloc]initWithFrame:CGRectMake(_provinceBtn.frame.origin.x, _provinceBtn.frame.origin.y,titleW , 216*kScale) style:UITableViewStyleGrouped];
                //            _provinceTableView.delegate = self;
                //            _provinceTableView.dataSource = self;
                //            [cell addSubview:_provinceTableView];
                _cityBtn = [ZYButton buttonWithType:UIButtonTypeRoundedRect];
                
                _cityBtn.frame = CGRectMake(30*kScale+textSize.width+58*kScale*2+btnW, 24*kScale, btnW, btnH);
                //            [_provinceBtn setImage:[UIImage imageNamed:@"选择省份"] forState:UIControlStateNormal];
                [_cityBtn setBackgroundImage:[UIImage imageNamed:@"选择省份"] forState:UIControlStateNormal];
               
                [_cityBtn addTarget:self action:@selector(chooseCity:) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:_cityBtn];
                _cTitleLable = [[UILabel alloc]init];
                _cTitleLable.frame = CGRectMake(0, 0, titleW, btnH);
                _cTitleLable.text = @"武汉";
                _cTitleLable.font = [UIFont systemFontOfSize:13];
                _cTitleLable.textColor = ZallColorFromRGB(0x9e9e9e);
                _cTitleLable.textAlignment = NSTextAlignmentCenter;
                _cTitleLable.adjustsFontSizeToFitWidth = YES;
                [_cityBtn addSubview:_cTitleLable];
            }
            else if(indexPath.section==4)
            {
                _phoneNumField = [[UITextField alloc]init];
                _phoneNumField.delegate = self;
                _phoneNumField.font = [UIFont systemFontOfSize:15];
                _phoneNumField.frame = CGRectMake(30*kScale+textSize.width+58*kScale, 0, kScreenW-30*kScale-textSize.width-58*kScale, 100*kScale);
                _phoneNumField.placeholder = @"请输入手机号码";
                _phoneNumField.keyboardType = UIKeyboardTypeNumberPad;
                [cell addSubview:_phoneNumField];
            }
            else if(indexPath.section==5)
            {
                _codeField = [[UITextField alloc]init];
                _codeField.delegate = self;
                _codeField.font = [UIFont systemFontOfSize:15];
                //暂时
                _codeField.frame = CGRectMake(30*kScale+textSize.width+58*kScale, 0, kScreenW-30*kScale-textSize.width-58*kScale, 100*kScale);
                _codeField.placeholder = @"请输入验证码";
                _codeField.keyboardType = UIKeyboardTypeNumberPad;
                [cell addSubview:_codeField];
                ZYButton *codeBtn = [ZYButton buttonWithType:UIButtonTypeCustom];
                
                
                codeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
                codeBtn.layer.cornerRadius  = 8*kScale;
                codeBtn.layer.masksToBounds = YES;
                codeBtn.layer.borderWidth = 1.0f;
                codeBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
                [codeBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
                CGSize codeBtnSize = [codeBtn sizeThatFits:CGSizeMake(MAXFLOAT, 60*kScale)];
                codeBtn.frame = CGRectMake(kScreenW-30*kScale-178*kScale, 20*kScale, codeBtnSize.width+10*kScale,60*kScale);
                [codeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                [codeBtn addTarget:self action:@selector(getCode) forControlEvents:UIControlEventTouchUpInside];
                //重置
                _codeField.frame = CGRectMake(30*kScale+textSize.width+58*kScale, 0, kScreenW-30*kScale-textSize.width-58*kScale-codeBtnSize.width-30*kScale, 100*kScale);
                [cell addSubview:codeBtn];
            }
            
        }

    }
    else if (tableView.tag == 21)
    {
        UILabel *provinceLable = [[UILabel alloc]init];
        provinceLable.frame = CGRectMake(0, 0, 174*kScale, 50*kScale);
        Province *p = _provinceArr[indexPath.row];
        provinceLable.text = p.name;
        provinceLable.font = [UIFont systemFontOfSize:15];
        provinceLable.textColor = ZallColorFromRGB(0x9e9e9e);
        provinceLable.textAlignment = NSTextAlignmentCenter;
        //字体大小随字数变化
        provinceLable.adjustsFontSizeToFitWidth = YES;
        
        [cell addSubview:provinceLable];
    }
    else if (tableView.tag == 22)
    {
        UILabel *cityLable = [[UILabel alloc]init];
        cityLable.frame = CGRectMake(0, 0, 174*kScale, 50*kScale);
        City *c = _cityArr[indexPath.row];
        cityLable.text = c.cityName;
        cityLable.font = [UIFont systemFontOfSize:15];
        cityLable.textColor = ZallColorFromRGB(0x9e9e9e);
        cityLable.textAlignment = NSTextAlignmentCenter;
        //字体大小随字数变化
        cityLable.adjustsFontSizeToFitWidth = YES;
        
        cell.textLabel.text = c.cityName;
        
        cell.textLabel.hidden = YES;
        [cell addSubview:cityLable];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag==20) {
        if (indexPath.section==0) {
            return 200*kScale;
        }
        return  100*kScale;
    }
    else
    {
        return 50*kScale;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return  0.01;
    }
    else if (section==1)
    {
        return 30*kScale;
    }
    return 1*kScale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (tableView.tag==20)
   {
        
   }
   else if (tableView.tag==21)
   {
       _provinceTableView.frame = CGRectMake(0, 0, 0, 0);
       _provinceBtnSelect = !_provinceBtnSelect;
       UITableViewCell *cell = (UITableViewCell*)[_provinceTableView cellForRowAtIndexPath:indexPath];
       
       Province *p = _provinceArr[indexPath.row];
       _pTitleLable.text = p.name;
       _pId = p.pId;
       NSLog(@"777777%@",_pId);
       _cityBtnSelect = YES;
       [self chooseCity:nil];
   }
   else if (tableView.tag==22)
   {
       _cityTableView.frame = CGRectMake(0, 0, 0, 0);
       _cityBtnSelect = !_cityBtnSelect;
       
       City *c = _cityArr[indexPath.row];
       _cTitleLable.text = c.cityName;
   }
}

//倒计时
- (void)timeRun
{
    __block NSInteger time = 29; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮的样式
                [_codeBtn setTitle:@"重新发送" forState:UIControlStateNormal];
                [_codeBtn setTitleColor:[UIColor colorWithRed:210/255.0 green:210/255.0 blue:210/255.0 alpha:1.0f] forState:UIControlStateNormal];
                _codeBtn.userInteractionEnabled = YES;
            });
            
        }else{
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮显示读秒效果
                [_codeBtn setTitle:[NSString stringWithFormat:@"%.2d", seconds] forState:UIControlStateNormal];
                [_codeBtn setTitleColor:[UIColor colorWithRed:171/255.0 green:147/255.0 blue:103/255.0 alpha:1.0f] forState:UIControlStateNormal];
                _codeBtn.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}


- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)nextClick
{
//    if (_nameField.text.length==0||_ideNumField.text.length==0||_phoneNumField.text.length==0||_codeField.text.length==0)
//    {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"请输入完整信息" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil];
//        [alert show];
//    }
//    else if (_codeField.text!=_codeStr)
//    {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"验证失败" message:@"请输入正确验证码" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil];
//        [alert show];
//    }
//    else
//    {
   
    [WDData sharedWDData].idcar = _ideNumField.text;
    [WDData sharedWDData].province = _pTitleLable.text;
    [WDData sharedWDData].city = _cTitleLable.text;
    [WDData sharedWDData].mobile = _phoneNumField.text;
    [WDData sharedWDData].sms = _codeField.text;
    [WDData sharedWDData].reallyName = _nameField.text;
        IdentitySecondViewController *nextVC = [[IdentitySecondViewController alloc]init];
        [self.navigationController pushViewController:nextVC animated:YES];
//    }
    
}
//选择省份
- (void)chooseProvince:(ZYButton*)sender
{
    if (_provinceBtnSelect==NO) {
        UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
        CGRect rect=[_provinceBtn convertRect: _provinceBtn.bounds toView:window];
        _provinceTableView.frame = CGRectMake(rect.origin.x, rect.origin.y-64+52*kScale, 174*kScale, 216*kScale);
        
        NSString *urlStr = [NSString stringWithFormat:@"%@/userCenter/getAllSheng",APIYIGOU];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:[UserData usertoken] forHTTPHeaderField:@"Auth"];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        [manager POST:urlStr parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            NSArray *arr = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            DYLog(@"123456%@",arr);
            [_provinceArr removeAllObjects];
            for (int i=0; i<arr.count; i++)
            {
                Province *p = [[Province alloc]initWithName:arr[i][@"regionName"] Id:arr[i][@"id"] cities:nil];
                [_provinceArr addObject:p];
            }
            [_provinceTableView reloadData];
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            [ViewFactory showErrorStrMsg:NETWORK_ERR];
        }];
    }
    else
    {
        _provinceTableView.frame = CGRectMake(0, 0, 0, 0);
    }
    _provinceBtnSelect = !_provinceBtnSelect;

}
//选择市区
- (void)chooseCity:(ZYButton*)sender
{
    NSString *urlStr = [NSString stringWithFormat:@"%@/userCenter/getAllCL?id=%@",APIYIGOU,_pId];
    DYLog(@"....%@",_pId);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[UserData usertoken] forHTTPHeaderField:@"Auth"];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:urlStr parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *arr = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        DYLog(@"123456%@",arr);
        [_cityArr removeAllObjects];
        for (int i=0; i<arr.count; i++)
        {
            City *c = [[City alloc]init];
            c.cityName = arr[i][@"regionName"];
            
            c.cityId = arr[i][@"id"];
            [_cityArr addObject:c];
            if (i==0) {
                _cTitleLable.text = c.cityName;
            }
        }
        
        [_cityTableView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
    }];
    if (_cityBtnSelect==NO) {
        UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
        CGRect rect=[_cityBtn convertRect: _cityBtn.bounds toView:window];
        _cityTableView.frame = CGRectMake(rect.origin.x, rect.origin.y-64+52*kScale, 174*kScale, 216*kScale);
        
        
        
    }
    else
    {
        _cityTableView.frame = CGRectMake(0, 0, 0, 0);
    }
    _cityBtnSelect = !_cityBtnSelect;
    
}

//获取验证码
- (void)getCode
{
    [self request];
}
- (void)request
{
//    //验证是否是手机号
//    NSString *regex =@"^((1[3-8][0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
//    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
//    BOOL isMatch = [pred evaluateWithObject:_phoneNumField.text];
//    if (isMatch==YES) {
        NSString *urlStr = [[NSString stringWithFormat:@"%@/sys/login/memberRegistr/sms/randomCode?mobile_no=%@",APIYIGOU,_phoneNumField.text]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
        ;
        
        DYBaseRequest *request = [DYBaseRequest new];
        request.requestUrl = urlStr;
        [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
            NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
            NSLog(@"222222%@",allDic);
            NSString *boolSuccess = [NSString stringWithFormat:@"%@",allDic[@"success"]];
            if ([boolSuccess isEqualToString:@"1"])
            {
                
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                //                    [defaults setObject:dataDic forKey:@"request"];
                _codeStr = allDic[@"data"];
                //测试
                //_codeField.text = _codeStr;
                [self timeRun];
                
            }
        } error:^(YTKBaseRequest *request) {
            [ViewFactory showInfoMsg:@"获取验证码失败"];
        } failure:^(YTKBaseRequest *request) {
            [ViewFactory showErrorStrMsg:NETWORK_ERR];
        }];
//    }
//    else
//    {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"请输入有效手机号码" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil];
//        [alert show];
//    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
